<?php
/**
 * 会员关注
 * @return
 * @author yuncran@126.com
 */

namespace app\model;

use app\BaseModel;
use app\CacheKey;
use app\FieldKey;

class UserFollowModel extends BaseModel
{

    protected $name = 'user_follow';

    protected $pk = "id";

    protected $append = ['user'];


    public function getUserAttr($value, $data)
    {
        return UserModel::where(['id' => $data['to_user_id']])->append(['level_result'])
            ->withAttr('level_result', function ($value, $data) {
                return UserLevelModel::where(['id' => $data['level']])->cache(CacheKey::level_title)->find();
            })->field(FieldKey::user_field_has_one)->find();
    }

    public function fans()
    {
        return $this->hasOne(UserModel::class,'id','user_id')->append(['level_result'])
            ->withAttr('level_result', function ($value, $data) {
                return UserLevelModel::where(['id' => $data['level']])->cache(CacheKey::level_title)->find();
            })->field(FieldKey::user_field_has_one);
    }

}